package com.scm.controller;
import com.scm.service.FactoryService;
import com.scm.pojo.PoItem;
import com.scm.pojo.PoMain;
import com.scm.service.PoItemService;
import com.scm.service.PoMainService;
import com.scm.service.ProductService;
import com.scm.service.VenderService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @ClassName ProcurementController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/11 23:44
 * @Version 1.0
 **/
@RestController
public class ProcurementController {

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private PoMainService poMainService;

    @Autowired
    private Util util;

    @Autowired
    private PoItemService  poItemService;

    @Autowired
    private ProductService productService;

    @Autowired
    private VenderService venderService;


    @RequestMapping("/addPoMain")
    public Result addProMain(@RequestBody PoMain poMain, HttpServletRequest request){
        String account = (String) request.getSession().getAttribute("account");
        poMain.setAccount(account);
        factoryService.addPoMain(poMain);
        return Result.ok(null);
    }

    @RequestMapping("/getAllPoMain")
    public QueryResult getAllPoMain(Integer currentPage, Integer pageSize){
        Map map = Page.get(currentPage,pageSize);
        List list = util.show(poMainService.getPagePoMain(map)) ;
        int total = poMainService.countPoMain();
        return QueryResult.ok(total,list);
    }

    @RequestMapping("/getPoItem")
    public Result getPoItem(String poID){
        List<PoItem> list = poItemService.getPoItem(poID);
        for (PoItem poItem:list){
            poItem.setProductCode(productService.getProductName(poItem.getProductCode()));
        }
        return Result.ok(list);
    }

    @RequestMapping("/updatePoMain1")
    public Result updatePoMain1(String poID){
        PoMain poMain = poMainService.findPoMain(poID);
        poMain.setItems(poItemService.getPoItem(poID));
        return Result.ok(poMain);
    }

    @RequestMapping("/getUpdateItem")
    public Result getUpdateItem(String poID){
        List<PoItem> list = poItemService.getPoItem(poID);
        for (PoItem poItem:list){
            poItem.setProductName(productService.getProductName(poItem.getProductCode()));
        }
        return Result.ok(list);
    }

    @RequestMapping("/updatePoMain2")
    public Result updatePoMain2(@RequestBody PoMain poMain){
        factoryService.updatePoMain(poMain);
        return Result.ok(null);
    }

    @RequestMapping("/deletePoMain")
    public Result deletePoMain(String poID){
        PoMain poMain = poMainService.getPoMain(poID);
        if (!poMain.getStatus().equals("1")){
            return Result.error("已出库或付款，无法删除");
        }else {
            factoryService.deletePoMain(poID);
            return Result.ok(null);
        }
    }

    @RequestMapping("selectPoMain")
    public QueryResult selectPoMain(Integer currentPage, Integer pageSize,String date,String poID,String status){
       Map map = Page.get(currentPage,pageSize);
        map.put("poID",poID);
        map.put("status",status);
        if (!date.isEmpty()){
            String[] split = date.split("~");
            map.put("minDate",split[0].trim()+" 00:00");
            map.put("maxDate",split[1].trim()+" 00:00");
        }
        List list = poMainService.getLikePoMain(map);
        int total = poMainService.getCountLikePoMain(map);
        list = util.show(list);
        return QueryResult.ok(total,list);
    }

    @RequestMapping("endPoMain")
    public QueryResult endPoMain(Integer currentPage, Integer pageSize,String payType){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        map.put("payType",payType);
        if (payType.equals("款到发货")){
            map.put("status","3");
        }else {
            map.put("status","4");
        }
        List<PoMain> list = poMainService.endPoMain(map);
        list=util.show(list);
        int total = poMainService.endCountPoMain(map);
        return QueryResult.ok(total,list);
    }
    @RequestMapping("realEndPoMain")
    public Result realEndPoMain(String  poID){
        poMainService.realEndPoMain(poID);
        return Result.ok(null);
    }
}
